<?php

class ActivityController extends Zend_Controller_Action
{
    public function init()
    {
		$auth = Zend_Auth::getInstance();

		if ( !$auth->hasIdentity() ) $this->_redirect('/auth');

		$this->usr = $this->view->usr = $auth->getIdentity();

		$this->db 	= Zend_Registry::get('db');
    }

    public function indexAction()
    {
    	if ( $this->getRequest()->isPost() ){

    		$data = array(
    						'Usrs_Id' 	=> $this->usr->Id,
    						'Lctns_Id' 	=> (int)$this->_getParam('Lctn'),
    						'Actvs_Id' 	=> (int)$this->_getParam('Actv'),
    						'Frnds' 	=> (int)$this->_getParam('Frnds'),
    						'In' 		=> new Zend_Db_Expr('NOW()'),
    						'Out' 		=> new Zend_Db_Expr('NOW() + INTERVAL '.(int)$this->_getParam('Time').' MINUTE')
    					);

    		$this->view->Posted = $this->db->insert( 'Vsts', $data );

    	}else{

    		$LL = isset( $_COOKIE['LL'] ) ? (int)$_COOKIE['LL'] : 0;

    		$this->view->Actvs = $this->db->fetchAll('
    								SELECT
    									*
    								FROM
    									Actvs');

    		if ( $LL ){
	    		$this->view->LL = $this->db->fetchRow('
						    								SELECT
						    									Id, CONCAT( Locality,", ",Country ) Title
						    								FROM
						    									Lctns
						    								WHERE
						    									Id = ?', $LL );
    		}

    		$this->view->Lctns = $this->db->fetchAll('
					    								SELECT
					    									DISTINCT
					    									Id, CONCAT( Locality,", ",Country ) Title
					    								FROM
					    									Vsts V
					    									LEFT JOIN Lctns L ON V.Lctns_Id = L.Id
					    								WHERE
					    									V.Usrs_Id = ?
					    									AND L.Id != ?
					    								ORDER BY
					    									V.In DESC', array( $this->usr->Id, $LL ) );
    	}
    }
}